<!-- 添加用箱人 -->
<template>
  <div class="new-boxman-dialog">
    <el-dialog
      width="900px"
      class=""
      top="6vh"
      title="添加用箱人"
      :visible="$store.state.dialog.newBoxman"
      @close="close">
      <el-row>
        <el-col :span="24">
          <el-form :model="order" :rules="rules" ref="order" label-width="100px" size="mini">
            <el-row>
              <el-col :span="12">
                <el-form-item label="客户账号" prop="userName">
                  <el-input v-model="order.userName" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="公司全称">
                  <el-input v-model="order.fullName" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="客户名称" prop="name">
                  <el-input v-model="order.name" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="操作员">
                  <el-select filterable v-model="order.salesmanId" placeholder="请选择操作员">
                    <el-option label="全部" value=""></el-option>
                    <el-option v-for="item in salesmanList" :key="item.id" :label="item.fullName" :value="item.salesmanId"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <!-- <el-col :span="12">
                <el-form-item label="助记码" prop="helpMark">
                  <el-input v-model="order.helpMark" auto-complete="off"></el-input>
                </el-form-item>
              </el-col> -->
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="手机" prop="mobile">
                  <el-input v-model="order.mobile" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <!-- <el-form-item label="邮箱" prop="email">
                  <el-input v-model="order.email" auto-complete="off"></el-input>
                </el-form-item> -->
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="联系方式">
                  <el-input v-model="order.contact" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="地址">
                  <el-input v-model="order.address" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="邮编">
                  <el-input v-model="order.zipCode" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="电话" prop="telephone">
                  <el-input v-model="order.telephone" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="传真">
                  <el-input v-model="order.fax" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="QQ" prop="qq">
                  <el-input v-model="order.qq" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="上港代码" prop="protocolCode">
                  <el-input v-model="order.protocolCode" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="一拼通编号">
                  <el-input v-model="order.yptCode" auto-complete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="相关图像">
                <div class="uploader">
                  <el-upload
                    action="http://fx.api.ypt56.net/api/image"
                    :show-file-list="true"
                    :data="uploadimgData"
                    :multiple="false"
                    name="Picture"
                    v-model="order.images"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="order.images" :src="order.images" class="avatar">
                    <i v-if="!order.images" class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </div>
              </el-form-item>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureAdd('order')" size="mini">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  insertcarManagerInfo
} from '../api/customerManagement'
export default {
  data () {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('客户账号不能为空'))
      } else {
        const reg = /^[a-zA-Z0-9_-]{4,16}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入长度在 4 到 16 位规范的客户账号'))
        }
      }
    }
    var checkQQ = (rule, value, callback) => {
      const reg = /^[1-9][0-9]{4,9}$/
      if (reg.test(value) || value === '') {
        callback()
      } else {
        return callback(new Error('请输入正确的QQ号'))
      }
    }
    // var checkEmail2 = (rule, value, callback) => {
    //   const reg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的邮箱'))
    //   }
    // }
    // var checkPhone = (rule, value, callback) => {
    //   const reg = /^([1-9]\d{10})$/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的手机号'))
    //   }
    // }
    // var checkPhone2 = (rule, value, callback) => {
    //   const reg = /0\d{2}-\d{7,8}/
    //   if (reg.test(value) || value === '') {
    //     callback()
    //   } else {
    //     return callback(new Error('请输入正确的座机号'))
    //   }
    // }
    return {
      order: {
        userName: '',
        name: '',
        fullName: '',
        // helpMark: '',
        mobile: '',
        contact: '',
        email: '',
        ddress: '',
        zipCode: '',
        telephone: '',
        fax: '',
        qq: '',
        salesmanId: '',
        protocolCode: '',
        images: ''
      },
      rules: {
        userName: [
          { required: true, validator: checkUsername, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入客户名称', trigger: 'blur' }
        ],
        protocolCode: [
          { required: true, message: '请输入上港代码', trigger: 'blur' }
        ],
        qq: [
          { validator: checkQQ, trigger: 'blur' }
        ]
        // telephone: [
        //   { validator: checkPhone2, trigger: 'blur' }
        // ],
        // mobile: [
        //   { validator: checkPhone, trigger: 'blur' }
        // ]
        // email: [
        //   { validator: checkEmail2, trigger: 'blur' }
        // ]
        // helpMark: [
        //   { required: true, message: '请输入助记码', trigger: 'blur' }
        // ]
      }
    }
  },
  props: {
    uploadimgData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    salesmanList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    close () {
      this.$store.commit('hide_new_boxman_dialog')
    },
    // 添加用箱人
    async addBoxman (formName) {
      const res = await insertcarManagerInfo(this.order)
      if (res.status === 200) {
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        this.$parent.getTabeDate(1, 15)
        this.resetForm(formName)
        this.close()
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
      }
    },
    beforeAvatarUpload (file) {
      this.uploadimgData.image = file.name.split('.')[0]
    },
    handleAvatarSuccess (res, file) {
      this.order.images = res.data[0].path
    },
    resetForm (formName) {
      this.order = {
        userName: '',
        name: '',
        fullName: '',
        mobile: '',
        contact: '',
        aemail: '',
        ddress: '',
        zipCode: '',
        telephone: '',
        fax: '',
        qq: '',
        protocolCode: '',
        salesmanId: '',
        images: ''
      }
      this.$refs[formName].resetFields()
    },
    // 确认添加
    sureAdd (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addBoxman(formName)
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.new-boxman-dialog {
  .uploader {
    width: 100px;
    height: 100px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .uploader:hover {
    border-color: #409EFF;
  }
  .uploader /deep/ .el-upload__input{
    display: none;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style>
